#include<fun.h>
#include"head.h"
#include"threadPool.h"
#include"taskQueue.h"

int func1(int netFd);//ls 列出相应目录文件 
int func2(char *pathBuf, int netFd);// cd 进入对应目录
int func3(int netFd);//pwd 显示目前所在路径
int func4(int netFd);//puts 将本地文件上传至客户端
int func5(int netFd);//gets 文件名 接收客户端文件到本地
int func6(int netFd);//删除文件
int func7(int netFd);//查看日志
//int func8(void);

void *threadFunc(void *parg){
    threadPool_t *pThreadPool = (threadPool_t *)parg;
    while(1){
        int netFd;
        pthread_mutex_lock(&pThreadPool->mutex);
        pthread_cleanup_push(unlock,&pThreadPool->mutex);
        while(pThreadPool->taskQueue.size == 0 && pThreadPool->isThreadPoolRunning == 1){
            pthread_cond_wait(&pThreadPool->cond,&pThreadPool->mutex);
        }
        if(pThreadPool->isThreadPoolRunning == 0){
            puts("child quit");
            pthread_exit(NULL);
        }
        puts("GetTask!");
        netFd = pThreadPool->taskQueue.pFront->netFd;
        deQueue(&pThreadPool->taskQueue);

        //服务器端登录验证代码段
        char name[1024]={0};
        char code[1024]={0};
        strcpy(name,"user2");
        bool across = false;
        while(across == false){
            int ret = recv(netFd,code,sizeof(code),0);
            //ERROR_CHECK(ret,-1,"recv");
            across = checkpwd(name,code);
            if(across == true){
                send(netFd,"true",sizeof(4),0);
            }
            else{
                send(netFd,"false",sizeof(5),0);
            }

        };

        //服务器端功能界面代码段
        int ret = 0;
        while(ret!=-1){
            ret = handleEvent(netFd);
        }

        //客户端断开连接，释放该线程
        close(0);
        pthread_cleanup_pop(1);
        printf("thread done! tid = %lu\n",pthread_self());
    }
}
//根据指令功选择调用
int handleEvent(int netFd){
    char command_buf[1024]={0};
    int ret = recv(netFd,command_buf,sizeof(command_buf),MSG_NOSIGNAL);
    if(ret == 0){puts("客户端关闭，结束连接");return -1;}
    int command = atoi(command_buf);

    if(command == 0){
        return 0;
    }
    printf("收到指令%d\n",command);

    char pathBuf[1024]={0};
    switch (command)
    {
    case 1:{
        func1(netFd);
        break;
    }

    case 2:
        func2(pathBuf, netFd);
        break;

    case 3:
        func3(netFd);
        break;

    case 4:
        func4(netFd);
        break;

    case 5:
        func5(netFd);
        break;

    case 6:
        func6(netFd);
        break;

    case 7:
        func7(netFd);
        break;

    case 8:
        //func8();
        break;
    
    default:
        printf("error\n");
        break;
    }
}


int func1(int netFd){
    ls_c(netFd);
}

int func2(char *pathBuf, int netFd){
    recv(netFd,pathBuf,sizeof(pathBuf),0);
    puts(pathBuf);
    cd_c(pathBuf, netFd);
}

int func3(int netFd){
    pwd(netFd);
}

int func4(int netFd){
    recvFile(netFd);
}

int func5(int netFd){
    char buf[1024]={0};
    bzero(buf,sizeof(buf));
    recv(netFd,buf,sizeof(buf),0);
    sendFile(buf,netFd);//传入文件名
}

int func6(int netFd){
    remove_net(netFd);
}

int func7(int netFd){
    viewlog(netFd);
}

//int func8(int netFd);